package com.example.prom.ok.y2023.trade.service;

import com.alibaba.fastjson.JSON;
import com.example.prom.ok.y2023.trade.vo.TradeDetailVO;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OuyiTradeService {

    @Autowired
    private RestTemplate restTemplate;

    private static final String BASE_URL = "https://www.ouyicn.blue";

    /**
     *
     * @param request
     * @param unit m H
     */
    public void getDetailTradeDataByTime(HttpServletRequest request, String unit){
        Asserts.notBlank(unit,"unit");
        HttpHeaders httpHeaders = new HttpHeaders();
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String key : parameterMap.keySet()) {
            String value = parameterMap.get(key)[0];
            httpHeaders.set(key, value.trim());
        }
        Asserts.notBlank(unit,"unit");
        String url = BASE_URL+"/priapi/v5/market/candles?instId=OKB-USDT&bar="+unit+"&after=&limit=280&t="+System.currentTimeMillis();


        HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
        // get请求如果想要设置请求头，需要使用exchange方法，因为getForxx方法不支持请求头
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        String body = exchange.getBody();
        TradeDetailVO tradeDetailVO = JSON.parseObject(body, TradeDetailVO.class);
        System.out.println(JSON.toJSONString(tradeDetailVO));
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        System.out.println(URLDecoder.decode("first_ref=https%3A%2F%2Fwww.ouyicn.blue%2Fcn%2Flearn%2Fzero-based-learning-market-analysis; u_pid=D6D6lm98EWLjBi5iBHvl; "));
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        System.out.println(JSON.toJSONString(list));
    }

}
